package com.yicong.ants.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.cchao.simplelib.core.AndroidHelper;
import com.cchao.simplelib.core.Logs;
import com.cchao.simplelib.core.RxBus;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.util.CallBacks;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.ad.AntsAd;
import com.yicong.ants.databinding.AdTimerBinding;
import com.yicong.ants.manager.ad.AntsAdHelper;
import com.zx.sdk.ZxSDK;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * 应用内广告落地view，展示在左上角 15 秒
 *
 * @author cchao
 * @version 2019-05-28.
 */
public class AdViewTimerView extends LinearLayout implements View.OnClickListener {
    AdTimerBinding mBinding;
    CompositeDisposable mDisposable;
    Context mContext;
    CallBacks.Int timeUpCallback;

    public void setTimeUpCallback(CallBacks.Int timeUpCallback) {
        this.timeUpCallback = timeUpCallback;
    }

    public AdViewTimerView(Context context) {
        this(context, null);
    }

    public AdViewTimerView setDisposable(CompositeDisposable disposable) {
        mDisposable = disposable;
        return this;
    }

    public AdViewTimerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.ad_timer, this, true);
        mBinding.setClick(this);
    }

    public void startTime(CompositeDisposable compositeDisposable) {
        AntsAd adInfo = AntsAdHelper.getAdInfo(AntsAdHelper.Position.All);
        if (adInfo == null) {
            return;
        }

        if (adInfo.getAd_id() > 10) {
            AntsAdHelper.exposeAd(String.valueOf(adInfo.getAd_id()));
        }

        UiHelper.setVisibleElseGone(mBinding.timer, true);
        int count_time = 16;
        mBinding.timer.setTag("0");
        mBinding.timer.setText(String.valueOf(count_time));
        compositeDisposable.add(Observable.interval(1, TimeUnit.SECONDS)
                .take(count_time)
                .map(a -> count_time - a - 1)
                .delay(1, TimeUnit.SECONDS)
                .compose(RxHelper.toMain())
                .subscribe(aLong -> {
                            //
                            mBinding.timer.setText(String.valueOf(aLong));
                            Logs.d(" AdViewTimerView " + aLong);
                        }
                        , RxHelper.getErrorConsumer()
                        , () -> {
                            Logs.d(" AdViewTimerView complete");
                            if (AndroidHelper.isContextDestroyed(mContext)) {
                                return;
                            }
                            mBinding.timer.setText("");
                            mBinding.timer.setBackgroundResource(R.drawable.ad_view_timer_close);
                            mBinding.timer.setTag("1");
                            RxBus.get().postEvent(Constants.Event.Ants_Ad_Complete, Collections.emptyMap());
                            ZxSDK.uploadHitInformation("0", "0", 0, 0);
                        }));
    }

    private Disposable timerDisposable;

    public void startTimeForNews(CompositeDisposable compositeDisposable, String newsId) {
        mDisposable = compositeDisposable;
        UiHelper.setVisibleElseGone(mBinding.timer, true);
        int count_time = 20;
        mBinding.timer.setTag("0");
        mBinding.timer.setText(String.valueOf(count_time));
        compositeDisposable.add(timerDisposable = Observable.interval(1, TimeUnit.SECONDS)
                .take(count_time)
                .map(a -> count_time - a - 1)
                .delay(1, TimeUnit.SECONDS)
                .compose(RxHelper.toMain())
                .subscribe(aLong -> {
                            //
                            mBinding.timer.setText(String.valueOf(aLong));
                            Logs.d(" AdViewTimerView " + aLong);
                        }
                        , RxHelper.getErrorConsumer()
                        , () -> {
                            Logs.d(" AdViewTimerView complete");
                            if (AndroidHelper.isContextDestroyed(mContext)) {
                                return;
                            }
                            mBinding.timer.setText("");
                            mBinding.timer.setBackgroundResource(R.drawable.ad_view_timer_close);
                            mBinding.timer.setTag("1");
                            RxBus.get().postEvent(Constants.Event.Ants_Ad_Complete_For_News, newsId);
                        }));
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.timer:
                if (mBinding.timer.getTag() == null) {
                    return;
                }

                if (mBinding.timer.getTag().equals("0")) {
                    UiHelper.showConfirmDialog(mContext, "倒计时结束才能领取奖励哦!", null);
                    return;
                }
                if (timeUpCallback != null) {
                    timeUpCallback.onCallBack(0);
                } else {
                    Context context = mBinding.timer.getContext();
                    if (context instanceof Activity) {
                        Activity activity = (Activity) context;
                        activity.finish();
                    }
                }
                break;
        }

    }

    public boolean isTimeUp() {
        return !"0".equals(mBinding.timer.getTag());
    }

    public void reset() {
        if (mDisposable != null && timerDisposable != null) {
            mDisposable.remove(timerDisposable);
        }
        mBinding.timer.setBackgroundResource(R.drawable.ad_view_timer_bg);
        mBinding.timer.setVisibility(View.GONE);
    }
}
